package com.kulian.Signalling.system.service.impl.mobile;

import com.kulian.Signalling.system.service.CallControlService;
import com.kulian.Signalling.system.service.MobileSignalingAdapter;
import com.kulian.Signalling.system.service.MobileTokenService;
import org.springframework.stereotype.Service;

@Service
public class MobileSignalingAdapterImpl implements MobileSignalingAdapter {

    private final CallControlService callControlService;
    private final MobileTokenService tokenService;

    public MobileSignalingAdapterImpl(CallControlService callControlService, 
                                   MobileTokenService tokenService) {
        this.callControlService = callControlService;
        this.tokenService = tokenService;
    }

    @Override
    public void handleMobileCallRequest(String mobileToken, String callee) {
        String caller = tokenService.validateToken(mobileToken);
        callControlService.initiateCall(caller, callee);
    }

    @Override
    public void handleMobileCallResponse(String mobileToken, String callId, String action) {
        String user = tokenService.validateToken(mobileToken);
        switch (action) {
            case "ACCEPT":
                callControlService.acceptCall(user, callId);
                break;
            case "REJECT":
                callControlService.rejectCall(user, callId, "User rejected");
                break;
            case "END":
                callControlService.endCall(user, callId);
                break;
        }
    }

    @Override
    public void handleMobileMediaOffer(String mobileToken, String callId, String sdp) {
        String user = tokenService.validateToken(mobileToken);
        callControlService.sendMediaOffer(user, callId, sdp);
    }

    @Override
    public void handleMobileIceCandidate(String mobileToken, String callId, String candidate) {
        String user = tokenService.validateToken(mobileToken);
        callControlService.sendIceCandidate(user, callId, candidate);
    }
}
